package com.alibaba.citrus.cr.b2b.trade.order.repository.impl;

import javax.annotation.Resource;

import com.alibaba.citrus.cr.b2b.trade.order.repository.DeliveryOrderDetailRepository;

import com.epoch.app.crb2btradecenter.domain.deliveryorderdetail.dto.CreateDeliveryOrderDetailResponse;
import com.epoch.app.crb2btradecenter.domain.deliveryorderdetail.dto.LoadByDeliveryOrderIdRequest;
import com.epoch.app.crb2btradecenter.domain.deliveryorderdetail.dto.LoadByDeliveryOrderIdResponse;
import com.epoch.app.crb2btradecenter.domain.deliveryorderdetail.dto.UpdateDeliveryOrderDetailResponse;
import com.epoch.app.crb2btradecenter.domain.deliveryorderdetail.model.DeliveryOrderDetail;
import com.epoch.app.crb2btradecenter.domain.deliveryorderdetail.service.DeliveryOrderDetailService;
import com.epoch.app.crb2btradecenter.sdo.DeliveryOrderDetailSDO;
import org.springframework.stereotype.Component;

@Component
public class DeliveryOrderDetailRepositoryImpl implements DeliveryOrderDetailRepository {

    @Resource
    private DeliveryOrderDetailService deliveryOrderDetailService;

    @Override
    public Boolean modify(DeliveryOrderDetailSDO deliveryOrderDetailSDO) {
        LoadByDeliveryOrderIdRequest loadByDeliveryOrderIdRequest = new LoadByDeliveryOrderIdRequest();
        loadByDeliveryOrderIdRequest.setDeliveryOrderId(deliveryOrderDetailSDO.getDeliveryOrderId());
        LoadByDeliveryOrderIdResponse loadByDeliveryOrderIdResponse = deliveryOrderDetailService.loadByDeliveryOrderId(loadByDeliveryOrderIdRequest);
        DeliveryOrderDetail result = loadByDeliveryOrderIdResponse.getResult();
        result.setDeliveryStatus(deliveryOrderDetailSDO.getDeliveryStatus());
        UpdateDeliveryOrderDetailResponse updateDeliveryOrderDetailResponse = deliveryOrderDetailService.updateDeliveryOrderDetail(result);
        if (updateDeliveryOrderDetailResponse.getCount() != 1) {
            throw new RuntimeException("modify deliveryOrderDetail effect rows not 1!");
        }
        return true;
    }

    @Override
    public String Create(DeliveryOrderDetailSDO deliveryOrderDetailSDO) {
        DeliveryOrderDetail deliveryOrderDetail = deliveryOrderDetailConverter(deliveryOrderDetailSDO);
        CreateDeliveryOrderDetailResponse response = deliveryOrderDetailService.createDeliveryOrderDetail(deliveryOrderDetail);
        return  response.getLastInsertId();
    }

    private DeliveryOrderDetail deliveryOrderDetailConverter(DeliveryOrderDetailSDO deliveryOrderDetailSDO) {
        DeliveryOrderDetail deliveryOrderDetail = new DeliveryOrderDetail();
        if (null != deliveryOrderDetailSDO) {
            deliveryOrderDetail.setId(deliveryOrderDetailSDO.getId());
            deliveryOrderDetail.setDeliveryOrderId(deliveryOrderDetailSDO.getDeliveryOrderId());
            deliveryOrderDetail.setTenantId(deliveryOrderDetailSDO.getTenantId());
            deliveryOrderDetail.setCustomerCode(deliveryOrderDetailSDO.getCustomerCode());
            deliveryOrderDetail.setDeliveryTime(deliveryOrderDetailSDO.getDeliveryTime());
            deliveryOrderDetail.setReceivedTime(deliveryOrderDetailSDO.getReceiveTime());
            deliveryOrderDetail.setOutPackageOrderId(deliveryOrderDetailSDO.getOutPackageOrderId());
            deliveryOrderDetail.setLogisticsNo(deliveryOrderDetailSDO.getLogisticsNo());
            deliveryOrderDetail.setExpressCompanyCode(deliveryOrderDetailSDO.getExpressCompanyCode());
            deliveryOrderDetail.setExpressCompanyName(deliveryOrderDetailSDO.getExpressCompanyName());
            //TODO 发货明细 Map
            deliveryOrderDetail.setSyncVersion(deliveryOrderDetailSDO.getSyncVersion());
            deliveryOrderDetail.setGmtCreate(deliveryOrderDetailSDO.getGmtCreate());
            deliveryOrderDetail.setGmtModified(deliveryOrderDetailSDO.getGmtModified());
            deliveryOrderDetail.setCreatorId(deliveryOrderDetailSDO.getCreatorCode());
            deliveryOrderDetail.setCreatorName(deliveryOrderDetailSDO.getCreator());
            deliveryOrderDetail.setModifierId(deliveryOrderDetailSDO.getUpdaterCode());
            deliveryOrderDetail.setModifierName(deliveryOrderDetailSDO.getUpdater());
        }

        return deliveryOrderDetail;
    }
}